﻿using ServiceCore.DbContext;
using ServiceCore.Entity.Core;
using ServiceCore.Entity.Dto;
using ServiceCore.Entity.Model;
using ServiceCore.Entity.Param.Tag;

namespace ServiceCore.Service
{
    /// <summary>
    /// 标签服务
    /// </summary>
    public partial class TagService : SqlsugarServices<TagInfo>
    {
        /// <summary>
        /// 分页 
        /// </summary>
        /// <param name="param"></param>
        /// <returns></returns>
        public BaseParam<TagInfo, List<TagInfo>> Tag_List(BaseParam<TagInfo, List<TagInfo>> arg)
        {
            arg.Data = this.Context.Queryable<TagInfo>()
                .WhereIF(arg.Param != null && arg.Param.TagTypeId > 0, x => x.TagTypeId == arg.Param.TagTypeId)
                .OrderBy(x => x.CreateTime)
                .ToList();
            arg.Param = null;

            return arg;
        }

        /// <summary>
        /// 是否存在
        /// </summary>
        /// <param name="param"></param>
        /// <returns></returns>
        public BaseParam<TagInfo, TagInfo> Tag_Exists(BaseParam<TagInfo, TagInfo> arg, bool? isAdd = null)
        {
            arg.Data = this.Context.Queryable<TagInfo>()
                .WhereIF(arg.Id > 0, x => x.Id == arg.Id)
                .WhereIF(arg.Param != null && string.IsNullOrEmpty(arg.Param.Name) == false, x => x.Name == arg.Param.Name)
                .WhereIF(isAdd.HasValue && isAdd == false, x => x.Id != arg.Param.Id)
                .First();

            return arg;
        }

        /// <summary>
        /// 下拉框
        /// </summary>
        /// <param name="param"></param>
        /// <returns></returns>
        public BaseParam<RecordInfo, List<DownBoxDto>> Tag_DownBox(BaseParam<RecordInfo, List<DownBoxDto>> arg, bool? isAdd = null)
        {
            arg.Data = this.Context.Queryable<TagTypeInfo>()
                .OrderBy(x => x.CreateTime)
                .Select(x => new DownBoxDto() { Text = x.Name,  Value = x.Id.ToString() })
                .ToList();
            if (arg.Data.Any() == false)
                return arg;

            var tags = this.Context.Queryable<TagInfo>()
                .OrderBy(x => x.CreateTime)
                .ToList();
            if (tags.Any() == false)
                return arg;

            var tagsTemp = new List<TagInfo>();
            arg.Data.ForEach(x => 
            {
                tagsTemp = tags.Where(n => n.TagTypeId.ToString() == x.Value).ToList();
                if (tagsTemp.Any())
                {
                    x.Children = new List<DownBoxDto>();
                    tagsTemp.ForEach(n => x.Children.Add(new DownBoxDto() { Text = n.Name, Value = n.Id.ToString() }));
                }
            });

            return arg;
        }
        

        /// <summary>
        /// 新增
        /// </summary>
        /// <param name="param"></param>
        /// <returns></returns>
        public BaseParam<TagInfo, long> Tag_Add(BaseParam<TagInfo, long> arg)
        {
            arg = Tag_Add_Verify(arg);
            if (string.IsNullOrEmpty(arg.ErrorMsg) == false)
                return arg;

            arg.Data = InsertReturnBigIdentity(arg.Param);
            arg.Param = null;

            return arg;
        }

        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="param"></param>
        /// <returns></returns>
        public BaseParam<TagInfo, long> Tag_Edit(BaseParam<TagInfo, long> arg)
        {
            arg = Tag_Edit_Verify(arg);
            if (string.IsNullOrEmpty(arg.ErrorMsg) == false)
                return arg;

            arg.Data = this.Context.Updateable<TagInfo>()
                .SetColumns(x => new TagInfo() { Name = arg.Param.Name })
                .Where(x => x.Id == arg.Param.Id)
                .ExecuteCommand();
            arg.Param = null;

            return arg;
        }

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="param"></param>
        /// <returns></returns>
        public BaseParam<TagDeleteParam, long> Tag_Delete(BaseParam<TagDeleteParam, long> arg)
        {
            arg = Tag_Del_Verify(arg);
            if (string.IsNullOrEmpty(arg.ErrorMsg) == false)
                return arg;

            arg.Data = this.Context.Deleteable<TagInfo>().Where(x => x.Id == arg.Id).ExecuteCommand();
            arg.Param = null;

            return arg;
        }
    }
}
